package campus.aiit.ssl.controller;

import campus.aiit.ssl.domain.model.*;
import campus.aiit.ssl.service.LocationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ihaokun
 * @date 2019/11/29 16:16
 */
@Api(description = "其他相关Api")
@RestController
public class LocationController {
  @Resource
  private LocationService locationService;

  @ApiOperation("校区(列表)")
  @GetMapping("schoolZones")
  public SchoolZone[] schoolZones(){
    return locationService.schoolZones();
  }

  @ApiOperation("校区(添加)")
  @PostMapping("schoolZones")
  public int addZone(@RequestParam String name){
    return locationService.addZone(name);
  }

  @ApiOperation("校区(修改)")
  @PutMapping("schoolZones")
  public int updateZone(@RequestBody SchoolZone zone){
    return locationService.updateZone(zone);
  }

  @ApiOperation("校区(删除)")
  @DeleteMapping("schoolZones")
  public int deleteZone(long id){
    return locationService.removeZone(id);
  }

  @ApiOperation("楼栋(添加)")
  @PostMapping("buildings")
  public int addBuilding(@RequestBody Building building){
    return locationService.addBuilding(building);
  }

  @ApiOperation("楼栋(修改)")
  @PutMapping("buildings")
  public int updateBuilding(@RequestBody Building building){
    return locationService.updateBuilding(building);
  }

  @ApiOperation("楼栋(列表)：根据校区Id")
  @GetMapping("buildings")
  public Building[] buildings(long zoneId) {
    return locationService.buildings(zoneId);
  }

  @ApiOperation("楼栋(列表)：根据校区名")
  @GetMapping("schoolZones/{name}/buildings")
  public Building[] listBuildingsByZoneName(@PathVariable String name) {
    return locationService.buildingsByZoneName(name);
  }

  @ApiOperation("楼栋(删除)")
  @DeleteMapping("buildings")
  public int deleteBuilding(long id) {
    return locationService.deleteBuilding(id);
  }

  @ApiOperation("楼层(查询)：根据楼栋Id")
  @GetMapping("buildings/{id}/floors")
  public int floors(@PathVariable long id) {
    return locationService.floors(id);
  }

  @ApiOperation("教室(列表)")
  @GetMapping("rooms")
  public Room[] getRooms(Long blockId) { return locationService.getRooms(blockId); }

  @ApiOperation("教室(添加)")
  @PostMapping("rooms")
  public int addRoom(@RequestBody Room room) {
    return locationService.addRoom(room);
  }

  @ApiOperation("教室(修改)")
  @PutMapping("rooms")
  public int updateRoom(@RequestBody Room room) {
    return locationService.updateRoom(room);
  }

  @ApiOperation("教室(删除)")
  @DeleteMapping("rooms")
  public int deleteRoom(long id) {
    return locationService.deleteRoom(id);
  }

  @ApiOperation("教室(列表)：根据楼栋Id/楼层")
  @GetMapping({"rooms/{buildingId}", "rooms/{buildingId}/{floor}"})
  public Room[] rooms(@PathVariable long buildingId, @PathVariable(required = false) Long floor) {
    if (floor != null) {
      return locationService.rooms(buildingId, floor);
    } else {
      return locationService.rooms(buildingId);
    }
  }


  @ApiOperation("区域类型(添加)")
  @PostMapping("roomType")
  public int addRoomType(@RequestBody Map map) {
    return locationService.addRoomType(map);
  }

  @ApiOperation("区域类型(删除)")
  @DeleteMapping("roomType")
  public int delRoomType(long id) {
    return locationService.delRoomType(id);
  }

  @ApiOperation("区域类型(修改)")
  @PutMapping("roomType")
  public int updateRoomType(@RequestBody Map map) {
    return locationService.updateRoomType(map);
  }

  @ApiOperation("区域类型(列表)")
  @GetMapping("roomType")
  public List<Map<String,Object>> getRoomTypeList(){
    return locationService.getRoomTypeList();
  }

  @ApiOperation("获取房间位置")
  @GetMapping("location")
  public SchoolZone location(long roomId){
    return locationService.location(roomId);
  }

  @ApiOperation("自控状态（修改）")
  @PutMapping("deviceAutoStatus")
  public int updateAutoStatus(@RequestBody DeviceAutoStatus record) {
    return locationService.updateAutoStatus(record);
  }

  @ApiOperation("自控状态(列表)")
  @GetMapping("deviceAutoStatus")//查询自控状态
  public Map<String, Object> deviceAutoStatus(Long zoneId,Long buildingId,Long floor,Long roomId,Long addressCode,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
    Map<String, Object> result = new HashMap<>();
    PageHelper.startPage(pageNum,pageSize);
    List<Map<String, Object>> devices = locationService.deviceAutoStatus(zoneId,buildingId,floor,roomId,addressCode);
    result.put("list", devices);
    PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(devices);
    result.put("total", pageInfo.getTotal());
    return result;
  }

  @ApiOperation("全局配置（修改）")
  @PutMapping("globalConfig")
  public int updateGlobalConfig(@RequestBody GlobalConfig record) {
    return locationService.updateGlobalConfig(record);
  }

  @ApiOperation("全局配置（列表）")
  @GetMapping({"globalConfig"})
  public GlobalConfig[] globalConfig(){
      return locationService.globalConfig();
  }

  @ApiOperation("历史状态(添加)")
  @PostMapping("dataLogs")
  public int addCollectLog(CollectLog collectLog) {
      return locationService.addCollectLog(collectLog);
  }

  @ApiOperation("历史状态(删除)")
  @DeleteMapping("dataLogs")
  public int deleteCollectLog(long id) {
        return locationService.deleteCollectLog(id);
    }

  @ApiOperation("历史状态(列表)")
  @GetMapping("dataLogs")
  public Map<String, Object> collectLogs(Long typeId,Long zoneId, Long buildingId, Long floor, Long roomId, Long deviceId,
                                         String startTime, String endTime, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
    Map<String,Object> result = new HashMap<>();
    PageHelper.startPage(pageNum,pageSize);
    List<Map<String, Object>> dataLogs = locationService.collectLogs(typeId,zoneId, buildingId, floor, roomId, deviceId, startTime, endTime);
    result.put("list", dataLogs);
    PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(dataLogs);
    result.put("total", pageInfo.getTotal());
    return result;
  }

  @ApiOperation("历史指令(添加)")
  @PostMapping("instructLogs")
  public int addInstructLog(InstructLog instructLog) {
      return locationService.addInstructLog(instructLog);
  }

  @ApiOperation("历史指令(删除)")
  @DeleteMapping("instructLogs")
  public int deleteInstructLog(long id) {
      return locationService.deleteInstructLog(id);
  }

  @ApiOperation("历史指令(列表)")
  @GetMapping("instructLogs")
  public Map<String, Object> instructLogs(Long zoneId, Long buildingId, Long floor, Long roomId, Long deviceId,
                             String startTime, String endTime, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
    Map<String,Object> result = new HashMap<>();
    PageHelper.startPage(pageNum,pageSize);
    List<Map<String, Object>> instructLogs = locationService.instructLogs(zoneId, buildingId, floor, roomId, deviceId, startTime, endTime);
    result.put("list", instructLogs);
    PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(instructLogs);
    result.put("total",pageInfo.getTotal());
    return result;
  }

  @ApiOperation("消息（列表）")
  @GetMapping("message")
  public Map<String,Object> Messages(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
    Map<String,Object> result = new HashMap<>();
    List<Map<String,Object>> messages = locationService.getMessages(pageNum,pageSize);
//    System.out.println("test"+messages);
    result.put("list",messages);
    result.put("total",locationService.countMessages());
    return result;
  }

  @ApiOperation("消息（删除）")
  @DeleteMapping("message/{ids}")
  public int removeMessage(@PathVariable long[] ids){return locationService.delMessage(ids);}

  @ApiOperation("消息（修改状态已读）")
  @PutMapping("message")
  public int modifyMessage(long id) { return locationService.modifyMessage(id); }

  @ApiOperation("报警类型（列表）")
  @GetMapping("alarms")
  public Alarm[] alarms(){return locationService.alarms();}

  @ApiOperation("报警类型（修改）")
  @PutMapping("alarms")
  public int modifyAlarm(@RequestBody Alarm alarm){ return locationService.modifyAlarm(alarm); }
}